//
//  URL.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "URL.h"
#import "Reachability.h"
#import "SaveTools.h"

@interface URL ()

@property (nonatomic, copy) NSString *urlString;

@end

@implementation URL
-(void)dealloc
{
    [_urlString release];
    Block_release(_finish);
    [self.reciveData release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.reciveData = [NSMutableData data];
    }
    return self;
}
// 判断网络是否连接成功
+ (BOOL)judgeConnection
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    if (status == NotReachable) { // 没网
        return NO;
    } else if (status == ReachableViaWWAN) { // 3G
        return YES;
    } else if (status == ReachableViaWiFi) { // wifi
        return YES;
    }
    return NO;
}

+(void)conectionWithUrl:(NSString *)url dic:(NSMutableDictionary *)dic  block:(void (^)(NSData *))block{
    
    
    
    
    
     NSString *par = @"";
    for (NSString *key in dic) {
        
        
        if (par.length == 0) {
            par = [NSString stringWithFormat:@"?%@=%@",key,[dic objectForKey:key]];
            
        }else{
            par = [NSString stringWithFormat:@"%@&%@=%@",par,key,[dic objectForKey:key]];
            
        }
    }
    
    NSString *string = [NSString stringWithFormat:@"%@%@",url,par];
    
    NSLog(@"解析网址 : %@",string);
    URL *asy = [[URL alloc]init];
    
    asy.finish = block;
    
    
    NSString *str = [NSString stringWithString:string];
    if (str.length > 100) {

        NSRange range = NSMakeRange(str.length - 100, 100);
        str = [str substringWithRange:range];
    }
    
    str= [str stringByReplacingOccurrencesOfString:@"/" withString:@"1"];
    str = [str stringByReplacingOccurrencesOfString:@":" withString:@"2"];
    str = [str stringByReplacingOccurrencesOfString:@"?" withString:@"3"];
    str = [str stringByReplacingOccurrencesOfString:@"%" withString:@"4"];
    str = [str stringByReplacingOccurrencesOfString:@"-" withString:@"5"];
    str = [str stringByReplacingOccurrencesOfString:@"_" withString:@"6"];

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *myPath = [array firstObject];
    myPath = [NSString stringWithFormat:@"%@/Caches/%@", myPath, str];
    
    
    NSLog(@"********** %@", myPath);
    
    asy.urlString = str;
    BOOL judge = [URL judgeConnection];
    if (judge) {
        [asy conection:string];

    }else{
        NSFileManager *manager = [NSFileManager defaultManager];
        if ([manager fileExistsAtPath:myPath]) {
            NSData *data = [SaveTools readDataWithName:str directory:NSLibraryDirectory];
            asy.reciveData = data.mutableCopy;
            asy.finish(asy.reciveData);
            NSLog(@"没网");
            
       
        
            
            
    }else{
            
            NSLog(@"1fdsfafds");
        }
    
    }
    
    [asy release];
    
    
}
/**************判断进来的字符串中有没有中文****************/
-(BOOL)IsChinese:(NSString *)str
{
    for(int i=0; i< [str length];i++)
    {
        
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;

        }

    } return NO;
}


- (void)conection:(NSString *)strUrl
{

    
    NSURL *url = [NSURL URLWithString:strUrl];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    
    [request setHTTPMethod:@"GET"];
    
    
    
    [NSURLConnection connectionWithRequest:request delegate:self];
    

    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
    NSHTTPURLResponse *curent = (NSHTTPURLResponse *)response;
    NSDictionary *dic = curent.allHeaderFields;
    
    
    
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    [self.reciveData appendData:data];
    

    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{ 
    self.finish(self.reciveData);
    [SaveTools saveDataWith:self.urlString directory:NSLibraryDirectory data:self.reciveData];
}





@end
